Imports System.Data.SqlClient
Public Class cls_tercerosubcategoria_

    Implements ICloneable

#Region "Variables ..."
    Private int_idtercerosubcategoria As Integer = 0
    Private int_idtercero As Integer = 0
    Private int_idsubcategoria As Integer = 0

    Private obj_subcategoria As cls_subcategoria_ = Nothing

    Private col_Items As Collections.Generic.List(Of cls_tercerosubcategoria_) = Nothing
#End Region

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idtercerosubcategoria
        End Get
    End Property
    Property idtercerosubcategoria() As Integer
        Get
            idtercerosubcategoria = int_idtercerosubcategoria
        End Get
        Set(ByVal value As Integer)
            int_idtercerosubcategoria = value
        End Set
    End Property
    Property idtercero() As Integer
        Get
            idtercero = int_idtercero
        End Get
        Set(ByVal value As Integer)
            int_idtercero = value
        End Set
    End Property
    Property idsubcategoria() As Integer
        Get
            idsubcategoria = int_idsubcategoria
        End Get
        Set(ByVal value As Integer)
            int_idsubcategoria = value
        End Set
    End Property

    Property subcategoria() As cls_subcategoria_
        Get
            If IsNothing(obj_subcategoria) Then
                obj_subcategoria = New cls_subcategoria_
                If Not obj_subcategoria.Buscar(idsubcategoria) Then
                    obj_subcategoria = Nothing
                End If
            End If
            subcategoria = obj_subcategoria
        End Get
        Set(ByVal value As cls_subcategoria_)
            obj_subcategoria = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_tercerosubcategoria_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_tercerosubcategoria_))
            col_Items = value
        End Set
    End Property
#End Region

    Public Overrides Function ToString() As String
        'Return "(" & subcategoria.categoria.ToString & ") - " & subcategoria.ToString

        Select Case str_formatoCombos
            Case "Nombre#########(15) - sigla(5)"
                Return subcategoria.ToString.PadRight(15) & " - " & subcategoria.categoria.ToString.PadRight(5)
            Case "sigla(5) - Nombre#########(15)"
                Return subcategoria.categoria.ToString.PadRight(5) & " - " & subcategoria.ToString.PadRight(15)
            Case "Nombre - sigla"
                Return subcategoria.ToString & " - " & subcategoria.categoria.ToString
            Case "sigla - Nombre"
                Return subcategoria.categoria.ToString & " - " & subcategoria.ToString
            Case Else
                Return subcategoria.categoria.ToString & " - " & subcategoria.ToString
        End Select
    End Function

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_tercerosubcategoria_)
        While obj_Reader.Read()
            Dim Elemento As New cls_tercerosubcategoria_
            Elemento.int_idtercerosubcategoria = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercerosubcategoria")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero"))) = False Then
                Elemento.int_idtercero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idsubcategoria"))) = False Then
                Elemento.int_idsubcategoria = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idsubcategoria")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idtercerosubcategoria = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercerosubcategoria")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero"))) = False Then
            int_idtercero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idsubcategoria"))) = False Then
            int_idsubcategoria = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idsubcategoria")))
        End If
    End Sub

    Public Function Nuevo(Optional ByVal ObtenerID As Boolean = False) As Boolean
        Nuevo = False
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "tercerosubcategoria__Nuevo"
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = int_idtercero
                .Parameters.Add("@idsubcategoria", SqlDbType.Int).Value = int_idsubcategoria
                .ExecuteNonQuery()
            End With
            Nuevo = True
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Crear el registro de tercerosubcategoria_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Function

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "tercerosubcategoria__Modificar"
                .Parameters.Add("@idtercerosubcategoria", SqlDbType.Int).Value = int_idtercerosubcategoria
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = int_idtercero
                .Parameters.Add("@idsubcategoria", SqlDbType.Int).Value = int_idsubcategoria
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Modificar el registro de tercerosubcategoria_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idtercerosubcategoria As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "tercerosubcategoria__Eliminar"
                .Parameters.Add("@idtercerosubcategoria", SqlDbType.Int).Value = idtercerosubcategoria
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Eliminar el registro de tercerosubcategoria_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idtercerosubcategoria As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "tercerosubcategoria__Buscar"
                .Parameters.Add("@idtercerosubcategoria", SqlDbType.Int).Value = idtercerosubcategoria
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar el registro de tercerosubcategoria_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "tercerosubcategoria__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarUltimo registro de tercerosubcategoria_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "tercerosubcategoria__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarTodos los registros de tercerosubcategoria_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function CargarXidtercero(ByVal idtercero As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidtercero = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "tercerosubcategoria__CargarXidtercero"
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = idtercero
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXidtercero = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarXidtercero los registros de tercerosubcategoria_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_tercerosubcategoria_Clon As New cls_tercerosubcategoria_
        obj_tercerosubcategoria_Clon.int_idtercerosubcategoria = Me.int_idtercerosubcategoria
        obj_tercerosubcategoria_Clon.int_idtercero = Me.int_idtercero
        obj_tercerosubcategoria_Clon.int_idsubcategoria = Me.int_idsubcategoria
        obj_tercerosubcategoria_Clon.col_Items = Me.col_Items
        Return obj_tercerosubcategoria_Clon
    End Function

End Class
